"""一些生成器方法，生成随机数，手机号，以及连续数字等"""
import random
from faker import Factory
from utils.id_card_num import IdCardNum

fake = Factory().create('zh_CN')
num_array = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
big_en_array = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
                'U', 'V', 'W', 'X', 'Y', 'Z']
small_en_array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
                  'u', 'v', 'w', 'x', 'y', 'z']


def random_phone_number():
    """随机手机号"""
    return fake.phone_number()


def random_name():
    """随机姓名"""
    return fake.name()


def random_address():
    """随机地址"""
    return fake.address()


def random_email():
    """随机email"""
    return fake.email()


def random_ipv4():
    """随机IPV4地址"""
    return fake.ipv4()


def random_vehicle_num():
    """随机生成车牌号码"""
    choices_cn = ['粤', '桂', '京', '湘', '苏', '辽']
    choice_cn_str = factory_choice_generator(choices_cn)()
    # 复制list到choices_en_array
    choices_en_array = big_en_array.copy()
    choices_en_array.remove("O")
    choices_en_array.remove("I")
    choice_en_str = factory_choice_generator(choices_en_array)()
    # 复制list到choice_num_array
    choice_num_array = num_array.copy()
    choice_num_array.extend(choices_en_array)
    choice_num_str = factory_choice_generator(choice_num_array)()
    num = ''
    for i in range(5):
        num += next(choice_num_str)

    return next(choice_cn_str) + next(choice_en_str) + num


def random_num(start=0, stop=10):
    """
    随机生成数字(默认生成0-9)
    @return int
    """
    return random.randrange(start, stop)


def random_num_str(start=0, stop=10):
    """
        随机生成数字(默认生成0-9)
        @return str
    """
    return str(random.randrange(start, stop))


def random_str(min_chars=0, max_chars=8):
    """长度在最大值与最小值之间的随机字符串"""
    return fake.pystr(min_chars=min_chars, max_chars=max_chars)


def random_id_card():
    """随机生成身份证号码"""
    return IdCardNum().gen_random_id_card()


def factory_generate_ids(starting_id=1, increment=1):
    """ 返回一个生成器函数，调用这个函数产生生成器，从starting_id开始，步长为increment。 """

    def generate_started_ids():
        val = starting_id
        local_increment = increment
        while True:
            yield val
            val += local_increment

    return generate_started_ids


def factory_choice_generator(values):
    """ 返回一个生成器函数，调用这个函数产生生成器，从给定的list中随机取一项。 """

    def choice_generator():
        my_list = list(values)
        # rand = random.Random()
        while True:
            yield random.choice(my_list)

    return choice_generator


if __name__ == '__main__':
    # print(random_phone_number())
    # print(random_name())
    # print(random_address())
    # print(random_email())
    # print(random_ipv4())
    # print(random_str(min_chars=6, max_chars=8))
    # id_gen = factory_generate_ids(starting_id=0, increment=2)()
    # for i in range(5):
    #     print(next(id_gen))
    #
    # choices = ['John', 'Sam', 'Lily', 'Rose']
    # choice_gen = factory_choice_generator(choices)()
    # print(next(choice_gen))
    # for i in range(10):
    #     print(next(choice_gen))

    print(random_vehicle_num())
    print(random_num_str(0, 3))
    print(random_id_card())
